package com.ytjj.qmyx.admin.service.imService.impl;

import com.ytjj.common.constants.im.ImRedisKeyConstant;
import com.ytjj.common.service.RedisService;
import com.ytjj.qmyx.admin.mapper.ImCustomerServiceTimeMapper;
import com.ytjj.qmyx.admin.model.ImCustomerServiceTime;
import com.ytjj.qmyx.admin.model.dto.ImCustomerServiceTimeDto;
import com.ytjj.qmyx.admin.model.response.ServiceTimeResponse;
import com.ytjj.qmyx.admin.model.vo.ImCustomerServiceTimeVO;
import com.ytjj.qmyx.admin.service.imService.ImCustomerServiceTimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ImCustomerServiceTimeServiceImpl implements ImCustomerServiceTimeService {

    @Resource
    private ImCustomerServiceTimeMapper imCustomerServiceTimeMapper;

    @Autowired
    private RedisService redisService;


    @Override
    public Integer addServiceTime(ImCustomerServiceTimeVO request) {
        ImCustomerServiceTime record = new ImCustomerServiceTime();
        record.setServiceName(request.getServiceName());
        String serviceTime = request.getServiceTime();
        record.setServiceTime(serviceTime);
        record.setServiceGroupName(request.getServiceGroupName());
        record.setServicePeriod(request.getServicePeriod());
        record.setCreateTime(new Date());
        int count = imCustomerServiceTimeMapper.insert(record);
        //把服务时间添加到redis中
        String[] serviceTimes = serviceTime.split("-");
        List<String> serviceTimeList =new ArrayList<>();
        for (String time : serviceTimes) {
            serviceTimeList.add(time);
        }
        //服务开始时间
        String startServiceTime = serviceTimeList.get(0);
        //服务结束时间
        String endServiceTime =serviceTimeList.get(1);
        //存入redis
        redisService.set(ImRedisKeyConstant.IM_START_SERVICE_TIME + request.getServiceName(), startServiceTime);
        redisService.set(ImRedisKeyConstant.IM_END_SERVICE_TIME + request.getServiceName(), endServiceTime);
        return count;
    }

    @Override
    public Integer deleteServiceTime(ImCustomerServiceTimeVO request) {
        int i = imCustomerServiceTimeMapper.deleteByPrimaryKey(request.getId());
        return i;
    }

    @Override
    public Integer updateServiceTime(ImCustomerServiceTimeVO request) {
        int count = imCustomerServiceTimeMapper.updateServiceTime(request);
        return count;
    }

    @Override
    public ServiceTimeResponse selectServiceTime() {

        ServiceTimeResponse serviceTimeResponse = new ServiceTimeResponse();

        List<ImCustomerServiceTimeDto> serviceTimeLists = imCustomerServiceTimeMapper.selectAll();

        for (ImCustomerServiceTimeDto serviceTime : serviceTimeLists) {
            String servicePeriod = serviceTime.getServicePeriod();
            if (!StringUtils.isEmpty(servicePeriod)){
                List<String> servicePeriods =new ArrayList<>();
                String[] servicePeriodStr = servicePeriod.split(",");
                for (String s : servicePeriodStr) {
                    servicePeriods.add(s);
                }
                serviceTime.setServicePeriods(servicePeriods);
            }
            String serviceGroupName = serviceTime.getServiceGroupName();
            if (!StringUtils.isEmpty(serviceGroupName)){
                List<String> serviceGroup = new ArrayList<>();
                String[] split = serviceGroupName.split(",");
                for (String s : split) {
                    serviceGroup.add(s);
                }
                serviceTime.setServiceGroup(serviceGroup);
            }
        }
        serviceTimeResponse.setImCustomerServiceTimeDtoList(serviceTimeLists);
        return serviceTimeResponse;
    }
}
